package com.zd.commodity.productMng.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.zd.commodity.dao.impl.BaseDao;
import com.zd.commodity.productMng.dao.IProductAttrValueDao;
import com.zd.commodity.productMng.entity.ProductAttrValue;

@Repository("productAttrValueDao")
public class ProductAttrValueDaoImpl extends BaseDao<ProductAttrValue> implements
		IProductAttrValueDao {

	@Resource(name = "sessionFactory")
	public void setSuperSessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
	@Override
	public DetachedCriteria getDetachedCriteria(ProductAttrValue t1,ProductAttrValue t2,Object params){
		DetachedCriteria dc=DetachedCriteria.forClass(ProductAttrValue.class);
		if(t1!=null){
			if(t1.getValue()!=null && !t1.getValue().equals("")){
				dc.add(Restrictions.like("value", t1.getValue(), MatchMode.ANYWHERE));
			}
			
			if(t1.getProductAttr()!=null){
				dc.createAlias("productAttr", "pa");
				if(t1.getProductAttr().getId()!=null && !t1.getProductAttr().getId().equals("")){
					dc.add(Restrictions.eq("pa.id", t1.getProductAttr().getId()));
				}
			}
			
			if(t1.getProduct()!=null){
				dc.createAlias("product", "p");
				if(t1.getProduct().getId()!=null && !t1.getProduct().getId().equals("")){
					dc.add(Restrictions.eq("p.id", t1.getProduct().getId()));
				}
				if(t1.getProduct().getName()!=null && !t1.getProduct().getName().equals("")){
					dc.add(Restrictions.like("p.name", t1.getProduct().getName(), MatchMode.ANYWHERE));
				}
				if(t1.getProduct().getInfoSource()!=null && !t1.getProduct().getInfoSource().equals("")){
					dc.add(Restrictions.like("p.infoSource", t1.getProduct().getInfoSource(), MatchMode.ANYWHERE));
				}
				
				
				if(t1.getProduct().getBrandShow()!=null){
					if(t1.getProduct().getBrandShow().getName()!=null && !t1.getProduct().getBrandShow().getName().equals("")){
						dc.createAlias("p.brandShow", "bs");
						dc.add(Restrictions.like("bs.name", t1.getProduct().getBrandShow().getName(), MatchMode.ANYWHERE));
					}
				}
				if(t1.getProduct().getCategory()!=null){
					if(t1.getProduct().getCategory().getName()!=null && !t1.getProduct().getCategory().getName().equals("")){
						dc.createAlias("p.category", "c");
						dc.add(Restrictions.like("c.name", t1.getProduct().getCategory().getName(), MatchMode.ANYWHERE));
					}
				}
				if(t1.getProduct().getProductSeries()!=null){
					if(t1.getProduct().getProductSeries().getName()!=null && !t1.getProduct().getProductSeries().getName().equals("")){
						dc.createAlias("p.productSeries", "ps");
						dc.add(Restrictions.like("ps.name", t1.getProduct().getProductSeries().getName(), MatchMode.ANYWHERE));
					}
				}
			}
		}
		return dc;
	}
	
	public ProductAttrValue findByProductId(Long productId){
		List<ProductAttrValue> list=getHibernateTemplate().find("from ProductAttrValue where product_id="+productId);
		if(list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}
}
